<?php

namespace app\controller;

use app\BaseController;
use app\model\User as UserModel;

class User extends BaseController
{
    public function login()
    {
        $post = request()->post();
        $user = UserModel::where("username", "=", $post['username'])->find();
        if (empty($user)) {
            return ["code" => 500, "msg" => "登录失败"];
        }
        if ($user->password != md5($post['password'])) {
            return ["code" => 500, "msg" => "密码错误"];
        }

        $expMinutes = 120 * 60 * 1000;
        $token = [
            "tokenName" => 'Authorization',
            "tokenValue" => 'Bearer ',
            "isLogin" => true,
            "loginId" => $user->id,
            "loginType" => 'login',
            "tokenTimeout" => $expMinutes,
            "sessionTimeout" => $expMinutes,
            "tokenSessionTimeout" => $expMinutes,
            "tokenActivityTimeout" => $expMinutes,
            "loginDevice" => '',
            "tag" => null
        ];
        return ["code" => 200, "data" => ["token" => $token, "userinfo" => $user], "msg" => "登录成功"];
    }

    public function register()
    {
        $post = request()->post();
        if (UserModel::create($post)) {
            return ["code" => 200, "data" => $post, "msg" => "注册成功"];
        } else {
            return ["code" => 500, "data" => [], "msg" => "注册失败"];
        }
    }

    public function logout()
    {
        return ["code" => 200, "data" => [], "msg" => "退出成功"];
    }

}
